package br.edu.ufam.avaliacao

import br.edu.ufam.academico.Curso

class RankingController {

    def index = {
        redirect(action: "ranking", params: params)
    }

    def ranking = {
        //params.max = Math.min(params.max ? params.int('max') : 5, 100)
        
        List<Avaliacao> avaliacoes = new ArrayList<Avaliacao>();
        
        
        //println ranking
        def avaliacaoInstance = Avaliacao.findAll("from Avaliacao where status='Finalizada' order by conceitoFinal") 
        def cursos = Avaliacao.executeQuery("select distinct curso from Avaliacao where status='Finalizada'")
        println cursos
        
        for(Curso curso:cursos){
            def avaliacao = Avaliacao.executeQuery("select a from Avaliacao a where curso=? and dateCreated=(select max(dateCreated) from Avaliacao where curso=?) and status='Finalizada'",[curso,curso])
            avaliacoes.add(avaliacao.get(0))           
        }
        
        Collections.sort(avaliacoes,new AvaliacaoComparator());
        
        //println avaliacoes
        
        
        
        //println avaliacoes
        //def ranking = avaliacaoInstance.list(sort:"conceitoFinal")
        
        
        [rankingInstanceList: avaliacoes, rankingTotal: avaliacoes.size()]
    }
    
    private class AvaliacaoComparator extends Comparator<Avaliacao>{
        public int compare(Avaliacao av1, Avaliacao av2){
            return av2.conceitoFinalDec.compareTo(av1.conceitoFinalDec);
        };
    }
}
